<% query = nil if query.nil? %>
<div class="searchBar">
  <div id="suggestionContainer" class="suggestionContainer">
    <%= render :partial => 'suggestion_box' %>
  </div>
  <script type="text/javascript">
    var suggestion_open = false;

    function suggest(query) {
      $('suggestionQuery').value = query;
      $('suggestionForm').onsubmit();
    }

    function openSuggest() {
      //open suggestion box
      jQuery('.suggestionContainer').show().fadeTo('normal', 1.0);
      suggestion_open = true;
    }

    function closeSuggest() {
      //close suggestion box
      jQuery(".suggestionContainer").fadeTo('normal', 0.0).hide();
      suggestion_open = false;
    }

    function updateOptions() {
      var queryString = '';
      // all words
      queryString += ' all(' + $('advopt_all').value + ')';
      // phrase
      queryString += ' phrase(' + $('advopt_phr').value + ')';
      // exclude
      queryString += ' exclude(' + $('advopt_exc').value + ')';
      // author
      queryString += ' author(' + $('advopt_aut').value + ')';
      // set new string to main field
      $('q').value = $('q').value + queryString;

      var optionString = '';
      // published on
      optionString += ' publ(' + $('advopt_pub').value + ')';
      // type: papers
      if ($('advopt_tpa').checked == true) { optionString += ' type1(true)'; } else { optionString += ' type1(false)'; }
      // type: books
      if ($('advopt_tbo').checked == true) { optionString += ' type2(true)'; } else { optionString += ' type2(false)'; }
      // type: popular
      if ($('advopt_tpo').checked == true) { optionString += ' type3(true)'; } else { optionString += ' type3(false)'; }
      // type: blog
      if ($('advopt_tbl').checked == true) { optionString += ' type4(true)'; } else { optionString += ' type4(false)'; }
      // type: news
      if ($('advopt_tne').checked == true) { optionString += ' type5(true)'; } else { optionString += ' type5(false)'; }
      // set new string to secondairy field
      $('o').value = optionString;

      return true;
    }
  </script>
  <div class="searchForm">
    <% form_tag({:controller => "search", :action => "search"}, :method => 'get', :id => 'searchbox', :onsubmit => 'updateOptions();') do %>
      <%= text_field_tag 'q', (query.nil?) ? '' : query[:search][:basic].join(' '), :onkeyup => "suggest(this.value);", :autocomplete => 'off' %>
      <%= hidden_field_tag 'o' %>
      <%= link_to 'Search', 'javascript:void(0);', :onclick => "$('searchbox').onsubmit(); $('searchbox').submit();" %>
    <% end %>
  </div>
  <div id="advancedContainer" class="advancedContainer">
    <div class="advancedSearch">
      <h2>Find scientific material that...</h2>
      <div class="colmnRow">
        <label>has all these words:</label>
        <div class="inputBox"><%= text_field_tag('advopt_all', (query.nil?) ? '' : query[:search][:advanced]['all'].join(' ')) %></div>
      </div>
      <div class="colmnRow">
        <label>has this exact wording or phrase:</label>
        <div class="inputBox"><%= text_field_tag('advopt_phr', (query.nil?) ? '' : query[:search][:advanced]['phrase'].join(' ')) %></div>
      </div>
      <div class="colmnRow">
        <label>excludes these words:</label>
        <div class="inputBox"><%= text_field_tag('advopt_exc', (query.nil?) ? '' : query[:search][:advanced]['exclude'].join(' ')) %></div>
      </div>
      <div class="leftColmn">
        <h2>I'm looking for...</h2>
        <div class="colmnRow">
          <label>this author:</label>
          <div class="inputBox"><%= text_field_tag('advopt_aut', (query.nil?) ? '' : query[:search][:advanced]['author'].join(' ')) %></div>
        </div>
        <div class="colmnRow">
          <label>published in:</label>
          <div class="inputBox"><%= text_field_tag('advopt_pub', (query.nil?) ? '' : query[:option]['publ'].join(' ')) %></div>
        </div>
      </div>
      <div class="rightColmn">
        <h2>I want to search in...</h2>
        <div class="colmnRow">
          <label>scientific papers</label>
          <div class="checkBox"><%= check_box_tag('advopt_tpa', '1', (query.nil?) ? true : query[:option]['type1']) %><%= image_tag('transparent.gif') %></div>
        </div>
        <div class="colmnRow">
          <label>scientific books</label>
          <div class="checkBox"><%= check_box_tag('advopt_tbo', '1', (query.nil?) ? false : query[:option]['type2']) %><%= image_tag('transparent.gif') %></div>
        </div>
        <div class="colmnRow">
          <label>popular science</label>
          <div class="checkBox"><%= check_box_tag('advopt_tpo', '1', (query.nil?) ? false : query[:option]['type3']) %><%= image_tag('transparent.gif') %></div>
        </div>
        <div class="colmnRow">
          <label>scientific blogs</label>
          <div class="checkBox"><%= check_box_tag('advopt_tbl', '1', (query.nil?) ? false : query[:option]['type4']) %><%= image_tag('transparent.gif') %></div>
        </div>
        <div class="colmnRow">
          <label>scientific news</label>
          <div class="checkBox"><%= check_box_tag('advopt_tne', '1', (query.nil?) ? false : query[:option]['type5']) %><%= image_tag('transparent.gif') %></div>
        </div>
      </div>
      <div class="break"></div>
    </div>
  </div>
</div>
<div class="searchExpand">
  <a href="javascript: void(0);" onclick="jQuery('#advancedContainer').slideToggle('normal'); jQuery('.searchExpand').toggleClass('advancedActive');">advanced options</a>
</div>
